package com.shanhai.wallpapers.fragments.wallpapers

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.switchMap
import androidx.lifecycle.viewModelScope
import androidx.paging.cachedIn
import com.shanhai.wallpapers.network.repository.PexelsRepository

class WallpaperViewModel(private val pexelsRepository: PexelsRepository) : ViewModel() {
    private val searchQuery = MutableLiveData(DEFAULT_QUERY)

    val wallpapers = searchQuery.switchMap { query ->
        pexelsRepository.getWallPapers(query).cachedIn(viewModelScope)
    }

    fun getWallpapers(query: String) {
        searchQuery.value = query
    }

    companion object {
        private const val DEFAULT_QUERY = "Nature"
    }
}